home *** CD-ROM | disk | FTP | other *** search
- Path: interramp.com!usenet
- From: us011245@interramp.com
- Newsgroups: comp.lang.c++
- Subject: Re: Virtual Base Class
- Date: Sun, 10 Mar 96 17:01:58 PDT
- Organization: PSI Public Usenet Link
- Message-ID: <NEWTNews.24289.826506668.hampton@interramp.com>
- References: <4i1gnv$b7i@uuneo.neosoft.com>
- NNTP-Posting-Host: ip108.herndon7.va.interramp.com
- Mime-Version: 1.0
- Content-Type: TEXT/PLAIN; charset=US-ASCII
- X-Newsreader: NEWTNews & Chameleon -- TCP/IP for MS Windows from NetManage
-
-
- In article <4i1gnv$b7i@uuneo.neosoft.com>, <Wmatthew@lan-aces.com> writes:
- >
- > In article <313F98D0.102E@ucla.edu>, Dennis Rahaman <dennisr@ucla.edu> says:
- > >
- > >This is what I want to do:
- > >
- > > a
- > > / \
- > > b c
- > > \ /
- > > d
- > >
- > >
- > >class a
- > > {
- > > //...
- > > };
- > >
- > >class b : public virtual a
- > > {
- > > //...
- > > };
- > >
- > >class c : public virtual b
- > > {
- > > //...
- > > };
- > >
- > >class d : public b, public c
- > > {
- > > //...
- > > };
- > >
- > >
- > >void f ()
- > > {
- > > a a1;
- > > d* pd = (d*) &a1; // error: can't cast virtual base to derived
- > > }
- > >
- > >///////////////////////////////////////////////////////////
- > >Can someone explain why I can't cast a virtual base class to a derived
- > >class?
- > >
- The way you've presented it, a1 is a member of class a. The fact that class a
- is a virtual base class for class d doesn't mean that all objects of class a
- are really objects of class d. I think you're confusing the idea of a virtual
- base class with the idea of a pure virtual function. In the sense of
- inheritance, what virtual does is to ensure that you don't get more than one
- copy of the base class in a multiple inheritance situation. For example, if
- you had left the virtual keyword out above, class d would have two copies of
- all class a's attributes--one copy inherited from class b and one copy
- inherited from class c. All that virtual does here is tell the compiler to use
- only one copy of class a, even though two are implied by the inheritance. This
- has nothing to do with whether objects of class a can exist that are not
- actually elements of class d.
-
- regards,
- Luther Hampton
-
-